/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.presenter;

import org.easypr.organizer.client.MyUserService;
import org.easypr.organizer.client.MyUserServiceAsync;
import org.easypr.organizer.client.event.DisplayInformationEvent;
import org.easypr.organizer.shared.MyUser;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class SubmenuPresenter.
 */
public class SubmenuPresenter implements Presenter {

	/**
	 * Define the interface the view needs to implement.
	 * 
	 * @author Anders
	 */
	public interface Display {
		
		/**
		 * Sets the data.
		 *
		 * @param user the new data
		 */
		void setData(MyUser user);

		/**
		 * As widget.
		 * 
		 * @return the widget
		 */
		Widget asWidget();
		
		/**
		 * Gets the logout link.
		 *
		 * @return the logout link
		 */
		Anchor getLogoutLink();
	}

	/** The event bus. */
	private final HandlerManager eventBus;
	
	/** The display. */
	private final Display display;

	/** The user. */
	MyUser user;
	
	/** The user rpc. */
	private MyUserServiceAsync userRPC;
	
	/**
	 * Instantiates a new menu presenter.
	 *
	 * @param eventBus the event bus
	 * @param view the view
	 * @param user the user
	 */
	public SubmenuPresenter(HandlerManager eventBus, Display view, MyUser user) {
		this.eventBus = eventBus;
		this.display = view;
		this.user = user;
	}
	

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.Presenter#go(com.google.gwt.user.client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		bind();
		updateContent();
	}
	
	/**
	 * Bind.
	 */
	private void bind() {
		display.getLogoutLink().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doLogout();
			}
		});
		
	}
	
	/**
	 * Do logout.
	 */
	public void doLogout() {
		if(userRPC == null) {
			userRPC = GWT.create(MyUserService.class);
		}
		AsyncCallback<MyUser> callback = new AsyncCallback<MyUser>() {
			public void onFailure(Throwable caught) {
			}

			public void onSuccess(MyUser user) {
				if(user instanceof MyUser) {
					AsyncCallback<MyUser> callback = new AsyncCallback<MyUser>() {
						public void onFailure(Throwable caught) {
							eventBus.fireEvent(new DisplayInformationEvent("Unable to proceed with the request!", DisplayInformationEvent.Severity.FAILURE));
						}

						public void onSuccess(MyUser user) {
							if(user != null) {
								Window.Location.replace(user.getLogoutURL());
								//History.newItem("dashboard");
							}
						}
					};
					userRPC.logout(callback);
					
				} 
			}
		};
		userRPC.getUser(callback);
	}
	
	/**
	 * Main function to update the content of the container.
	 */
	public void updateContent() {
		display.setData(this.user);
	}
}
